<template>
  <view>
    <!-- 面包屑 -->
    <uni-breadcrumb separator=">">
      <uni-breadcrumb-item v-for="(route,index) in routes" :key="index" :to="route.to">
        {{route.name}}
      </uni-breadcrumb-item>
    </uni-breadcrumb>


    <view class="goods-list">
      <view v-for="(goods, i) in list" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>

      </view>
    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        dbName: '',
        list: [],
        total: 0,
        pagenum: 1,
        // 是否正在请求数据
        isloading: false,
        // 每页显示多少条数据
        pagesize: 10,
        routes: [{
            to: "/pages/recreation/recreation",
            name: "A页面",
          },
          {
            to: "/B",
            name: "B页面",
          },
          {
            to: "/C",
            name: "C页面",
          },
        ]
      };
    },

    onLoad(option) {

      this.dbName = option.db
      this.getGoodsList(this.dbName)
    },
    methods: {
      getGoodsList(dbName, cb) {
        // ** 打开节流阀
        cb && cb()
        this.isloading = true

        const db = uniCloud.database();
        db.collection(dbName).get() //获取数据表的信息
          .then(res => {
            this.list = [...this.list, ...res.result.data]
            this.total = this.list.length
          })
          .catch(err => {
            console.log(err)
            uni.$showMsg()
          })
        // ** 关闭节流阀
        this.isloading = false


      },
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/buy/buy?goods_id=' + item.cid
        })
      }
    },
    // 触底的事件
    onReachBottom() {

      if (this.pagenum * this.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')

      if (this.isloading) return

      // 让页码值自增 +1
      this.pagenum += 1
      // 重新获取列表数据
      this.getGoodsList(this.dbName)
    },
    onPullDownRefresh() {
      // 1. 重置关键数据
      this.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []

      // 2. 重新发起请求
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
